package com.rmfyzxfw.caseprocess.boot.result;


import cn.hutool.core.util.StrUtil;

import java.text.MessageFormat;

/**
 * @author lixiaoxiang
 * @Description
 * @createTime 2021年12月24日 17:46:00
 */
public enum CommonStatusCode {
    SUCCESS(0, "处理成功", false),
    ERROR(-1, "操作失败", false),
    _1001(1001, "网络连接错误", false),
    _1002(1002, "网络连接超时", false),
    _1003(1003, "服务器内部错误", false),
    _1004(1004, "服务器不支持当前请求", true),
    _1005(1005, "网关错误", false),
    _1006(1006, "服务器无法处理当前请求", true),
    _1007(1007, "系统繁忙", true),
    _1008(1008, "中间件异常", false),
    _1009(1009, "数据库异常", false),
    _1010(1010, "其他异常", false),
    _1011(1011, "重复操作", false),
    _1012(1012, "令牌错误", false),
    _1013(1013, "令牌过期", false),
    //网关层使用
    _1014(1014, "无权限访问", false),
    //用于404时使用
    _1015(1015, "请求地址不存在", false),
    JURY_UNAUTHORIZED(1016, "陪审员服务登录异常", false),
    JURY_INTERNAL_SERVER_ERROR(1017, "陪审员服务内部错误", false),
    _2000(2000, "认证超时", true),
    //业务层使用
    _2001(2001, "没有权限访问", true),
    _2002(2002, "身份认证失败", true),
    _2003(2003, "用户不存在", true),
    _2004(2004, "角色不存在", true),
    _2005(2005, "参数格式异常", true),
    _2006(2006, "{0}字段不能为空", true),
    _2007(2007, "{0}字段不符合日期规则", true),
    _2008(2008, "{0}字段不符合数值规则", true),
    _2009(2009, "{0}字段不符合金额规则", true),
    _2010(2010, "{0}字段长度不符合规范", true),
    _2011(2011, "数据签名验签失败", false),
    _2012(2012, "存在重复数据", true),
    _2013(2013, "文件格式不正确", true),
    _2014(2014, "文件内容太大", true),
    _2015(2015, "参数不正确", true),
    _2016(2016, "新密码与当前密码一致", true),
    _3001(3001, "插入数据异常", false),
    _3002(3002, "修改数据异常", false),
    _3003(3003, "删除数据异常", false),
    _3004(3004, "查询数据异常", false),
    _3005(3005, "数据库连接异常", false),
    _3006(3006, "事务超时", false),
    _3007(3007, "数据库无法获取连接", false),
    _4001(4001, "访问外部服务异常：连接不上", false),
    _4002(4002, "访问外部服务异常：访问超时", false),
    _4003(4003, "访问外部服务异常：请求异常", false),
    _5001(5001, "资源已失效，请刷新重试", true),

    /**
     * 错误码及其描述
     * 示例：(5001, "错误描述，{0}是被替换占位符")
     */
    _6001(6001, "未登陆或登录超时！", true),
    _7001(7001, "{0}参数不正确", true),
    _7002(7002, "{0}已存在", true),
    _7003(7003, "{0}不存在", true),
    _7004(7004, "{0}参数值不正确，请使用以下值：{1}", true),
    _7005(7004, "{0}保存失败", true),

    _8001(8001, "{0}", true);

    private final int code;
    private final String desc;
    private final boolean businessException;

    CommonStatusCode(final int code, final String desc, final boolean businessException) {
        this.code = code;
        this.desc = desc;
        this.businessException = businessException;
    }

    public int getCode() {
        return this.code;
    }

    public String getDesc() {
        return this.desc;
    }

    public boolean isBusinessException() {
        return this.businessException;
    }

    public static String getCodeMsg(CommonStatusCode statusCode, String... formatMsgValue) {
        String msg = statusCode.getDesc();
        if (formatMsgValue.length > 0) {
            if (!StrUtil.contains(msg, "{0}")) {
                msg = StrUtil.isBlank(formatMsgValue[0]) ? msg : formatMsgValue[0];
            } else {
                Object[] array = new Object[formatMsgValue.length];

                System.arraycopy(formatMsgValue, 0, array, 0, formatMsgValue.length);
                msg = MessageFormat.format(statusCode.getDesc(), array);
            }
        }
        return msg;
    }
}
